﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace NMigrate
{
	public class BatchFileHandler : FileHandler, IHasOutput
	{
		
		public BatchFileHandler()
		{
			HasOutput.ConfigDefault(this);
		}
		#region FileHandler Members

		public override string Extension
		{
			get
			{
				return "bat";
			}
		}

		public override void Execute(System.IO.FileInfo file, Configuration args)
		{
			var process = new Process();
			process.StartInfo.FileName = file.FullName;
			process.StartInfo.RedirectStandardOutput = false;
			process.StartInfo.RedirectStandardError = false;
			process.StartInfo.UseShellExecute = false;
			process.Start();
			process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
			process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
			process.WaitForExit();
		}

		void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
		{
			if(Out != null)
				Out.WriteLine(e.Data);
		}

		void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
		{
			if(Error != null)
				Error.WriteLine(e.Data);
		}


		public TextWriter Out
		{
			get;
			set;
		}

		public TextWriter Error
		{
			get;
			set;
		}

		#endregion


		#region FileHandler Members


		public override bool IsTemplated(FileInfo file)
		{
			return true;
		}

		#endregion
	}
}
